package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.Role;
import com.heima.pojo.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.dto.PageUserQueryDTO;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.UserVo;
import com.heima.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {

        if (dto == null) {
            //参数异常
        }

//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(User::getLoginName,dto.getLoginName());
//        User user = userMapper.selectOne(wrapper);
        User user = userMapper.userGetLogin(dto);

        if (user == null) { //代表用户不存在得

        }
        if (!user.getPassword().equals(dto.getPassword())) { //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    @Override
    public PageResult userQuery(PageUserQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<User> page = userMapper.userQuery(dto);
        List<Role> roleList = roleMapper.roleQueryAll();
        for (User user : page) {
            for (Role role : roleList) {
                if (user.getRoleId().equals(role.getRoleId())) {
                    user.setRole(role);
                }
            }
        }
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }


    @Override
    public Boolean save(User user) {
        int result = userMapper.insert(user);
        return result != 0;
    }

    @Override
    public Boolean update(Integer userId, User user) {
        user.setId(userId);
        int result = userMapper.updateById(user);
        return result != 0;
    }

    @Override
    public Boolean delete(Integer userId) {
        int result = userMapper.deleteById(userId);
        return result != 0;
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public PageResult searchUserWork(PageUserQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<UserVo> page = userMapper.userPageQuery(dto);
        List<UserVo> userVoList = userMapper.userCount();
        for (UserVo userVo : page) {
            for (UserVo vo : userVoList) {
                    if (vo.getUserId().equals(userVo.getUserId())) {
                        userVo.setUserId(vo.getUserId());
                        userVo.setWorkCount(vo.getWorkCount());
                        userVo.setProgressTotal(vo.getProgressTotal());
                        userVo.setCancelCount(vo.getCancelCount());

                }
            }
        }
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }
}
